package com.chianghao.rock.system.service.dictionary_value;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chianghao.core.exception.HaoException;
import com.chianghao.core.frame.service.BaseService;
import com.chianghao.core.mybatis.sql.SqlWrapper;
import com.chianghao.core.mybatis.sql.db_enum.SqlOperators;
import com.chianghao.rock.system.dao.dictionary.DictionaryMapper;
import com.chianghao.rock.system.dao.dictionary_value.DictionaryValueMapper;
import com.chianghao.rock.system.entity.Dictionary;
import com.chianghao.rock.system.entity.DictionaryValue;

@Service
public class DictionaryValueService extends BaseService<DictionaryValueMapper, DictionaryValue> {
	
	
	
	
	
	private final ReentrantLock lock = new ReentrantLock();
	
	@Autowired
	DictionaryMapper dictionaryMapper;
	
	
	private int countParnentCodeAndValueCount(String code,Integer value) {
		DictionaryValue where  = new DictionaryValue();
		where.setDictionaryCode(code);
		where.setValue(value);
		String count = this.mapper.queryObjectByEntity("count(1)",where).toString();
		return Integer.parseInt(count);
	}
	
	
	private int countParnentCodeAndValueCount(String code,Integer value,Long id) {
		SqlWrapper sqlWrapper = new SqlWrapper()
		.addCondition(DictionaryValue::getDictionaryCode,code)
		.addCondition(DictionaryValue::getValue, value)
		.addCondition(DictionaryValue::getId,SqlOperators.not_equals, id);
		int count =  this.mapper.count(sqlWrapper);
		return count;
	}
	
	
	private boolean isExistCode(String code) {
		Dictionary where = new Dictionary();
		where.setCode(code);
		int count =Integer.parseInt(dictionaryMapper.queryObjectByEntity("count(1)", where).toString()) ;
		if(count>0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 添加时检查code内是否有重复的value
	 * @param dictionaryValue
	 */
	public void doCreate(DictionaryValue dictionaryValue) {
		//校验code是否存在
		if(!isExistCode(dictionaryValue.getDictionaryCode())) {
			throw new HaoException("error:code,not.valid.value");
		}
		try {
			lock.lock();
			if(countParnentCodeAndValueCount(dictionaryValue.getDictionaryCode(),dictionaryValue.getValue())>0) {
				throw new HaoException("error:value,not.repeatable");
			}
			this.insert(dictionaryValue);
		}finally {
			lock.unlock();
		}
		
	}

	public void doModify(DictionaryValue dictionaryValue) {
		dictionaryValue.setDictionaryCode(null);
		if(dictionaryValue.getValue()!=null) {
			DictionaryValue d = this.queryOneById(dictionaryValue.getId());
			try {
				lock.lock();
				if(countParnentCodeAndValueCount(d.getDictionaryCode(),dictionaryValue.getValue(),dictionaryValue.getId())>0) {
					throw new HaoException("error:value,not.repeatable");
				}
				this.updateById(dictionaryValue,dictionaryValue.getId());
			}finally {
				lock.unlock();
			}
		}else {
			this.updateById(dictionaryValue,dictionaryValue.getId());
		}
	}


	public List<DictionaryValue> queryByCode(String code) {
		DictionaryValue where  = new DictionaryValue();
		where.setDictionaryCode(code);
		return this.query(where);
	}


}